<template>
  <a-modal
    :title="title"
    :width="700"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="标识符">
          <a-input
            placeholder="请输入标识如：A- 或 1-等"
            @change="systemPrompt"
            v-decorator="[
              'identCode',
              {
                rules: [
                  { required: false, message: '请输入标识!' },
                  { max: 10, message: '标识编号最大长度为10个字符' },
                ],
              },
            ]"
          />
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="限制位数">
          <a-input-number
            style="width: 150px"
            placeholder="限制位数，默认为3"
            :min="1"
            :max="limitNumberMax"
            @change='numbersChange'
            v-decorator="['limitNumber', {rules: [{ required: true, message: '大小在1-5之间!'},{max:5,type:'number',message: '大小在1-5之间!'}],initialValue:3 }]"
          />
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="起始编号">
          <a-input-number
            style="width: 150px"
            :min="0"
            :max="999999"
            placeholder="请输入起始编号"
            v-decorator="['startNumber', { rules: [{ required: true, message: '请输入起始编号!' }] }]"
            @change="startNumberChange"
          />
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="数量">
          <a-input-number
            style="width: 150px"
            :min="1"
            :max="999999"
            placeholder="请输入数量"
            v-decorator="['numbers', validatorRules.numbers]"
            @change="numbersChange"
          />
        </a-form-item>
        <div v-if="isystemPrompt" style="margin-left: 10%; color: red">
          <span class="explain">{{ isystemPromptext }} </span>
        </div>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { getAction, httpAction } from '@api/manage'

export default {
  name: 'AddModalForm',
  data() {
    return {
      form: this.$form.createForm(this),
      title: '',
      visible: false,
      confirmLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      model: {},
      url: {
        add: '',
      },
      areaIdOptions: [],
      parkInfoList: [],
      parkCode: '',
      isystemPrompt: false,
      isystemPromptext: '',
      type:"",
      locateName:"",
      projectId:"",
      validatorRules:{
        numbers:{
          rules:[{
            required: true, message: '请输入数量!'},
            {
            validator:this.validateNumber
          }]
        }
      },
      limitNumberMax:5
    }
  },
  mounted() {

  },
  methods: {
    validateNumber(rule,value,callback){
      if (value&&Number(value)>100){
        callback('输入数量超过100')
      }else{
        callback()
      }
    },

    startNumberChange(value) {
      this.systemPrompt()
    },

    numbersChange(value) {
      let startNumberValue = this.form.getFieldValue('startNumber')
      if(startNumberValue === null || startNumberValue === undefined || startNumberValue ===""){
        this.$message.error('请先输入起始编号!')
        return
      }
      this.systemPrompt()
    },
    systemPrompt() {
      this.$nextTick(() => {
        this.isystemPrompt = false
        let identCode = this.form.getFieldValue('identCode'), // 区号
          startNumber = this.form.getFieldValue('startNumber'), //
          numbers = this.form.getFieldValue('numbers'),
          limitNumber = this.form.getFieldValue('limitNumber'),
          endNumber = startNumber + numbers - 1 //
        if (this.isEmpty(startNumber) || this.isEmpty(numbers)) {
          return
        }
        if(limitNumber>this.limitNumberMax){
          return
        }
        if(Number(endNumber) > 999){
          this.isystemPrompt = true
          this.isystemPromptext = "当前计算的末位号是"+endNumber+"，创建的号码不能大于999！"
          return
        }
        this.isystemPrompt = true
        let isystemPromptext,
          startNumberNum = String(startNumber).length,
          endNumberNum = String(endNumber).length
        while (String(startNumber).length<limitNumber)
        {
          startNumber = '0'+String(startNumber);
        }
        while (String(endNumber).length<limitNumber)
        {
          endNumber = '0'+String(endNumber);
        }
        // debugger
        if (identCode==undefined){
          isystemPromptext = `提示：系统将批量创建起始为${startNumber} ~ ${endNumber} ，共${numbers}个`
          // }
        }else {
          isystemPromptext = `提示：系统将批量创建起始为${identCode}${startNumber} ~ ${identCode}${endNumber} ，共${numbers}个`
          // }
        }
        this.isystemPromptext = isystemPromptext
      })
    },
    /**
     * 判断变量是否为空，
     * @param  {[type]}  param 变量
     * @return {Boolean}      为空返回true，否则返回false。
     */
    isEmpty(param) {
      if (param) {
        var param_type = typeof param
        if (param_type == 'object') {
          //要判断的是【对象】或【数组】或【null】等
          if (typeof param.length == 'undefined') {
            if (JSON.stringify(param) == '{}') {
              return true //空值，空对象
            }
          } else if (param.length == 0) {
            return true //空值，空数组
          }
        } else if (param_type == 'string') {
          //如果要过滤空格等字符
          var new_param = param.trim()
          if (new_param.length == 0) {
            //空值，例如:带有空格的字符串" "。
            return true
          }
        } else if (param_type == 'boolean') {
          if (!param) {
            return true
          }
        } else if (param_type == 'number') {
          if (!param) {
            return true
          }
        }
        return false //非空值
      } else {
        //空值,例如：
        //(1)null
        //(2)可能使用了js的内置的名称，例如：var name=[],这个打印类型是字符串类型。
        //(3)空字符串''、""。
        //(4)数字0、00等，如果可以只输入0，则需要另外判断。
        if(param===0){
          //非空
          return false
        }
        return true
      }
    },


    setId(id,num){
      this.form.resetFields()
      this.locateName=""
      this.projectId=""
      this.visible = true
      this.id = id
      if (num==1){
        this.url.add='/building/add'
      }else if (num == 2){
        this.url.add='/unit/add'
      }else if(num ==3){
        this.url.add='/floor/add'
      }
    },
    setHouse(id,projectId,locateName){
      this.form.resetFields()
      this.visible = true
      this.locateName = locateName
      this.id = id
      this.projectId = projectId
      this.url.add='/house/add'
      this.type="house"
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          let formData = Object.assign(this.model, values)
          if (that.locateName!=""){
            formData.locateName = that.locateName
          }
        if (that.projectId!=""){
          formData.projectId = that.projectId
        }
          formData.id = this.id;
          this.confirmLoading = true
          httpAction(that.url.add, formData, 'post')
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                if (that.type=="house"){
                  that.$emit('ok')
                }
                that.close()
              } else {
                that.$message.warning(res.message)
                that.close()
              }
            })
            .finally(() => {
              this.confirmLoading = false
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('ok',)
      this.visible = false
      this.isystemPrompt = false
    },

  },
}
</script>

<style scoped>
</style>
